]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/MainShip.cs
Chubas's House happened.
[rbdr/super-polarity] / Super Polarity / MainShip.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
8
9 namespace SuperPolarity
10 {
11 class MainShip
12 {
13 public Texture2D PlayerTexture;
14 public Vector2 Position;
15 public Vector2 Origin;
16 public bool Active;
17 public int Lives;
18 public int Multiplier;
19 public int Score;
20 public float Angle;
21
22 // Physics Properties
23 Vector2 Velocity;
24 Vector2 Acceleration;
25
26 float MaxVelocity;
27 float AccelerationRate;
28 ParticleEngine particleEngine;
29
30 public int Width
31 {
32 get { return PlayerTexture.Width; }
33 }
34
35 public int Height
36 {
37 get { return PlayerTexture.Height; }
38 }
39
40 public void Initialize(ContentManager Content, Texture2D texture, Vector2 position)
41 {
42 PlayerTexture = texture;
43 Position = position;
44 Active = true;
45 Multiplier = 1;
46 Lives = 3;
47 Score = 0;
48
49 Origin = new Vector2(PlayerTexture.Width / 2, PlayerTexture.Height / 2);
50 Velocity = new Vector2(0, 0);
51 Acceleration = new Vector2(0, 0);
52
53 MaxVelocity = 5;
54 AccelerationRate = 10;
55
56 List<Texture2D> texturesList = new List<Texture2D>();
57 texturesList.Add(Content.Load<Texture2D>("Graphics\\circle"));
58 texturesList.Add(Content.Load<Texture2D>("Graphics\\diamond"));
59 texturesList.Add(Content.Load<Texture2D>("Graphics\\star"));
60
61 particleEngine = new ParticleEngine(texturesList, Position);
62
63 BindInput();
64 }
65
66 void BindInput()
67 {
68 InputController.Bind("moveX", HandleHorizontalMovement);
69 InputController.Bind("moveY", HandleVerticalMovement);
70 }
71
72 public void HandleHorizontalMovement(float value)
73 {
74 Acceleration.X = value * AccelerationRate;
75 }
76
77 public void HandleVerticalMovement(float value)
78 {
79 Acceleration.Y = value * AccelerationRate;
80 }
81
82 public void AutoDeccelerate(GameTime gameTime)
83 {
84 if (Acceleration.X == 0 && Velocity.X > 0) {
85 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X)
86 {
87 Velocity.X = 0;
88 Acceleration.X = 0;
89 }
90 else
91 {
92 Acceleration.X = -AccelerationRate;
93 }
94 }
95
96 if (Acceleration.X == 0 && Velocity.X < 0)
97 {
98 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X)
99 {
100 Velocity.X = 0;
101 Acceleration.X = 0;
102 }
103 else
104 {
105 Acceleration.X = AccelerationRate;
106 }
107 }
108
109 if (Acceleration.Y == 0 && Velocity.Y > 0)
110 {
111 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y)
112 {
113 Velocity.Y = 0;
114 Acceleration.Y = 0;
115 }
116 else
117 {
118 Acceleration.Y = -AccelerationRate;
119 }
120 }
121
122 if (Acceleration.Y == 0 && Velocity.Y < 0)
123 {
124 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y)
125 {
126 Velocity.Y = 0;
127 Acceleration.Y = 0;
128 }
129 else
130 {
131 Acceleration.Y = AccelerationRate;
132 }
133 }
134 }
135
136 public void Update(GameTime gameTime)
137 {
138 Move(gameTime);
139 ChangeAngle();
140 particleEngine.EmitterLocation = Position;
141 particleEngine.Update();
142 }
143
144 public void Move(GameTime gameTime)
145 {
146 AutoDeccelerate(gameTime);
147
148 Velocity.X = Velocity.X + Acceleration.X * (float) gameTime.ElapsedGameTime.TotalSeconds;
149 Velocity.Y = Velocity.Y + Acceleration.Y * (float) gameTime.ElapsedGameTime.TotalSeconds;
150
151 if (Velocity.X > MaxVelocity)
152 {
153 Velocity.X = MaxVelocity;
154 }
155
156 if (Velocity.X < -MaxVelocity)
157 {
158 Velocity.X = -MaxVelocity;
159 }
160
161 if (Velocity.Y > MaxVelocity)
162 {
163 Velocity.Y = MaxVelocity;
164 }
165
166 if (Velocity.Y < -MaxVelocity)
167 {
168 Velocity.Y = -MaxVelocity;
169 }
170
171 Position.X = Position.X + Velocity.X;
172 Position.Y = Position.Y + Velocity.Y;
173 }
174
175 public void ChangeAngle()
176 {
177 Angle = (float) Math.Atan2(Velocity.Y, Velocity.X);
178 }
179
180 public void Draw(SpriteBatch spriteBatch)
181 {
182 particleEngine.Draw(spriteBatch);
183 spriteBatch.Draw(PlayerTexture, Position, null, Color.White, Angle, Origin, 1f, SpriteEffects.None, 0f);
184 }
185 }
186 }